/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.MeshStateViewState;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;
import br.ufmg.dees.insane.xyplot.xyplot_view.dialog.DecimalField;

/**
 * A class representing creation dialog on screen.<br>
 * @author Penna, Samuel & Pitangueira, Roque
 * @since september 2004
 */
public final class MeshStateScalesDialog extends JFrame {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private DesktopPostp parent;
	private JPanel mainPane = new JPanel();
	
	private DecimalField xScaleField;
	private DecimalField yScaleField;
	private DecimalField zScaleField;
	
	private JButton viewButton; 
	private JButton resetButton;
	private JButton okButton;
	private JButton cancelButton;
	
	private NumberFormat numberFormat;
	private ViewState state;
	
//	*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.Interface
	 */
	public MeshStateScalesDialog(DesktopPostp parent, ViewState vs) {
    	Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		
		this.parent = parent;
		this.state = ((MeshStateViewState)((PostpViewState)vs).getMeshStateViewState());
		
		this.setSize(new Dimension(175,225));
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		this.setLocationRelativeTo(this);
		
		setUpFormat();
		
		okButton = new JButton("    OK    ");
		cancelButton = new JButton("Cancel");
		
		// Sets up the grid panel
		JPanel sPane = new JPanel();
		sPane.setLayout(new BoxLayout(sPane, BoxLayout.Y_AXIS));
		sPane.add(mainPane);
		mainPane.setLayout(new GridLayout(4,2,2,2));
		mainPane.setMaximumSize(new Dimension(175,150));
		mainPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Scales"),
				BorderFactory.createEmptyBorder(2,2,2,2)));
		xScaleField = new DecimalField(((MeshStateViewState)this.state).getXScale(),10,numberFormat);
		xScaleField.setCaretColor(Color.GREEN);
		xScaleField.setMaximumSize(new Dimension(75,25));
		xScaleField.setPreferredSize(new Dimension(75,25));
		xScaleField.setMinimumSize(new Dimension(75,25));
		xScaleField.setSize(new Dimension(75,25));  
		xScaleField.setBackground(Color.BLACK);
		xScaleField.setForeground(Color.GREEN);
		xScaleField.setEditable(true);
		mainPane.add(new JLabel(" X scale:"));
		mainPane.add(xScaleField);
		yScaleField = new DecimalField(((MeshStateViewState)this.state).getYScale(),10,numberFormat);
		yScaleField.setCaretColor(Color.GREEN);
		yScaleField.setMaximumSize(new Dimension(75,25));
		yScaleField.setPreferredSize(new Dimension(75,25));
		yScaleField.setMinimumSize(new Dimension(75,25));
		yScaleField.setSize(new Dimension(75,25));  
		yScaleField.setBackground(Color.BLACK);
		yScaleField.setForeground(Color.GREEN);
		yScaleField.setEditable(true);
		mainPane.add(new JLabel(" Y scale:"));
		mainPane.add(yScaleField);
		zScaleField = new DecimalField(((MeshStateViewState)this.state).getZScale(),10,numberFormat);
		zScaleField.setCaretColor(Color.GREEN);
		zScaleField.setMaximumSize(new Dimension(75,25));
		zScaleField.setPreferredSize(new Dimension(75,25));
		zScaleField.setMinimumSize(new Dimension(75,25));
		zScaleField.setSize(new Dimension(75,25));  
		zScaleField.setBackground(Color.BLACK);
		zScaleField.setForeground(Color.GREEN);
		zScaleField.setEditable(true);
		mainPane.add(new JLabel(" Z scale:"));
		mainPane.add(zScaleField);
		viewButton = new JButton("  View  ");
		resetButton = new JButton("  Reset ");
		mainPane.add(viewButton);
		mainPane.add(resetButton);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		
		
		viewButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						View(event);
					}
				});
		
		resetButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						Reset(event);
					}
				});
		
		okButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						approvedPerform();
						Close(event);
					}
				});
		
		cancelButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						Close(event);
					}
				});
		
		buttonPane.add(okButton);
		buttonPane.add(cancelButton);
		
		JPanel generalPane = new JPanel();
		generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
		generalPane.add(mainPane);
		generalPane.add(buttonPane);
		this.add(generalPane);
		this.setVisible(true);
		
	}
	
	
//	*****************************************************************************
	public void Close(ActionEvent event){
		this.dispose();
	}
	
	public void View(ActionEvent event){
		((MeshStateViewState)state).setXScale(this.xScaleField.getValue());
		((MeshStateViewState)state).setYScale(this.yScaleField.getValue());
		((MeshStateViewState)state).setZScale(this.zScaleField.getValue());
		((InterfacePostp)this.parent.getDesktop().getSelectedFrame()).getView().getController().compose();
		((InterfacePostp)this.parent.getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
	}
	
	public void Reset(ActionEvent event){
		this.xScaleField.setValue(1);
		this.yScaleField.setValue(1);
		this.zScaleField.setValue(1);
		((MeshStateViewState)state).setXScale(1);
		((MeshStateViewState)state).setYScale(1);
		((MeshStateViewState)state).setZScale(1);
		((InterfacePostp)this.parent.getDesktop().getSelectedFrame()).getView().getController().compose();
		((InterfacePostp)this.parent.getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
	}
	
	/**
	 * This method is executed on OK button click.
	 *
	 */
	public void approvedPerform() {
		try{
			((MeshStateViewState)state).setXScale(this.xScaleField.getValue());
			((MeshStateViewState)state).setYScale(this.yScaleField.getValue());
			((MeshStateViewState)state).setZScale(this.zScaleField.getValue());
			((InterfacePostp)this.parent.getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
		} catch (Exception err) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
	/**
	 * Sets the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMaximumFractionDigits(3);
	}
	
//	*****************************************************************************
}
